package com.nf.exam.dao;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.nf.exam.entity.ProductEntity;
import com.nf.exam.util.ConnectionHelper;

public class ProductDao {

	private static Connection con=ConnectionHelper.getConnection();
	
	public static List<ProductEntity> getAll() throws SQLException{
		String sql="select * from exam";
		QueryRunner qr=new QueryRunner(true);
		return qr.query(con,sql,new BeanListHandler<ProductEntity>(ProductEntity.class));
	}
	
	public static int deletePrductId(int id) throws SQLException {
		QueryRunner qr=new QueryRunner(true);
		String sql="delete from exam where proid=?";
		return qr.update(con, sql, id);
	}
	
	public static int insertProduct(ProductEntity pro) throws SQLException {
		QueryRunner qr=new QueryRunner(true);
		String sql="insert into exam values(?,?,?,?,?)";
		return qr.update(con,sql,pro.getProid(),pro.getProname(),pro.getPrice(),pro.getAmount(),pro.getDescription());
	}
	
	public static int updateProduct(ProductEntity pro) throws SQLException {
		String sql="update exam set proname=?,price=?,amount=?,description=? where proid=?";
		QueryRunner qr=new QueryRunner(true);
		return qr.update(con, sql, pro.getProname(),pro.getPrice(),pro.getAmount(),pro.getDescription(),pro.getProid());
	}
}
